/* Import modules */
var async = require('async');

/* Counter */
var concurrencyCount = 0;
/* fetchUrl */
var fetchUrl = function(url, callback) {
	var delay = parseInt((Math.random() * 10000000) % 2000, 10);
	++concurrencyCount;
	console.log('concurrencyCount: ' + concurrencyCount + '. ',
		'url: ' + url + '. ',
		'delay: ' +  delay + ' msec.');
	setTimeout(function() {
		--concurrencyCount;
		callback(null, url + ' html content');
	}, delay);
};

var urls = [];
for (var i = 0; i < 30; ++i) {
	urls.push('https://datasource_' + i);
}
async.mapLimit(urls,
	5,
	function(url, callback) {
		fetchUrl(url, callback);
	},
	function(err, result) {
		console.log('Final:');
		console.log(result);
	});
